package com.zhoudb.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zhoudb.blog.entity.ArticlePicture;
import com.zhoudb.blog.service.ArticlePictureService;
import com.zhoudb.blog.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UeditorController {

    @Autowired
    private ArticlePictureService articlePictureService;

    @ResponseBody
    @RequestMapping(value = "ueditor")
    public String ueditor(@RequestParam(value = "action", required = true) String action,
                          @RequestParam(value = "noCache", required = false) String nocache,
                          @RequestParam(value = "upfile", required = false) MultipartFile upfile,
                          @RequestParam(value = "start", required = false) Integer start,
                          @RequestParam(value = "size", required = false) Integer size,
                          HttpServletRequest request, HttpServletResponse response) {
        String callback = null;
        switch (action) {
            //获取配置
            case "config":
                try {
                    response.setContentType("application/json;charset=utf-8");
                    Resource resource = new ClassPathResource("config.json");
                    File file = resource.getFile();
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    callback = stringBuilder.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                    callback = "error";
                }
                break;
            //上传图片
            case "uploadimage":
                Map<String, String> map = new HashMap<>();
                ArticlePicture articlePicture = new ArticlePicture();
                String url="http://"+QiniuUtil.preUrl + "/" + QiniuUtil.upload(upfile);
                articlePicture.setPictureUrl(url);
                articlePicture.setCreateBy(LocalDateTime.now());
                try {
                    articlePictureService.save(articlePicture);
                    map.put("url",url);
                    map.put("state", "SUCCESS");
                    map.put("title", upfile.getName());
                    map.put("original", upfile.getOriginalFilename());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                callback = new Gson().toJson(map);
                break;
            //图片空间
            case "listimage":
                Map<String, Object> map1 = new HashMap<>();
                QueryWrapper<ArticlePicture> query = new QueryWrapper<>();
                List<ArticlePicture> list = articlePictureService.list(query);
                map1.put("state", "SUCCESS");
                map1.put("list", list);
                map1.put("start", start);
                map1.put("size", size);
                callback = new Gson().toJson(map1);
                break;
            default:
                break;
        }
        return callback;
    }


}
